<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" label-position="left">

      <el-form-item label="省份" prop="province">
        <el-select v-model="formData.province" placeholder="请选择省份" clearable :style="{ width: '100%' }" @change="getStationList">
          <el-option v-for="(item, index) in provinceList" :key="index" :label="item.stationName" :value="item.stationId" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="试验站" prop="experimentStation">
        <el-select v-model="formData.experimentStation" placeholder="请选择试验站" clearable :style="{ width: '100%' }" @change="getCountyList" :disabled="stationSelectorDisabled">
          <el-option v-for="(item, index) in stationList" :key="index" :label="item.stationName" :value="item.stationId" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="示范县" prop="county">
        <el-select v-model="formData.county" placeholder="请选择示范县" clearable :style="{ width: '100%' }" :disabled="countySelectorDisabled">
          <el-option v-for="(item, index) in countyList" :key="index" :label="item.stationName" :value="item.stationName" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="年份" prop="year">
        <el-select v-model="formData.year" placeholder="请选择年份" clearable :style="{ width: '100%' }">
          <el-option v-for="(item, index) in yearList" :key="index" :label="item.label" :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" :style="{ margin:'2px'}">搜索</el-button>
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:station:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="recordList" row-key="recordId">
      <el-table-column prop="litchiVarieties" label="荔枝品种" align="center" :show-overflow-tooltip="true" width="160"></el-table-column>

      <el-table-column prop="provinceName" label="省份" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="testStationName" label="试验站" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="demonstrationCountyName" label="示范县" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="year" label="年份" :show-overflow-tooltip="true" width="160"></el-table-column>

      <el-table-column label="成熟期" align="center" prop="theEarliestMaturity">
        <template slot-scope="scope">
          <span>{{ scope.row.theEarliestMaturity }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>

</template>

<script>
import { listStation, listProvince } from "@/api/system/station";
import { listRecord } from "@/api/system/record";
export default {
  name: "Station",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 总条数
      total: 0,
      // 试验站表格数据
      stationList: [],
      // 示范县列表
      countyList: [],
      // 记录列表
      recordList: [],
      // 省份列表
      provinceList: [],
      // 是否显示弹出层
      open: false,

      stationSelectorDisabled: true,
      countySelectorDisabled: true,

      formData: {
        province: null,
        experimentStation: null,
        county: null,
        year: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        varityName: undefined,
        parentId: 0,
        demonstrationCountyName: null,
        year: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        province: [
          {
            required: true,
            message: "请选择省份",
            trigger: "change",
          },
        ],
        experimentStation: [
          {
            required: true,
            message: "请选择试验站",
            trigger: "change",
          },
        ],
        county: [
          {
            required: true,
            message: "请选择示范县",
            trigger: "change",
          },
        ],
      },
      yearList: [
        { label: "2010", value: 2010 },
        { label: "2011", value: 2011 },
        { label: "2012", value: 2012 },
        { label: "2013", value: 2013 },
        { label: "2014", value: 2014 },
        { label: "2015", value: 2015 },
        { label: "2016", value: 2016 },
        { label: "2017", value: 2017 },
        { label: "2018", value: 2018 },
        { label: "2019", value: 2019 },
        { label: "2020", value: 2020 },
        { label: "2021", value: 2021 },
        { label: "2022", value: 2022 },
      ],
    };
  },

  created() {
    this.getList();
  },

  methods: {
    /** 查询试验站和品种列表 */
    getList() {
      this.loading = true;

      listStation(this.queryParams).then((response) => {
        console.log(response);
        this.provinceList = response.rows;
        this.loading = false;
      });
    },

    /** 查询指定省份的试验站列表 选择省份时触发 */
    getStationList() {
      this.formData.experimentStation = this.formData.county = null;
      this.loading = false;
      this.queryParams.parentId = this.formData.province;
      listStation(this.queryParams).then((response) => {
        console.log(response);
        this.stationList = response.rows;
        this.loading = false;
      });
    },

    /** 查询指定试验站的示范县列表 选择试验站时触发 */
    getCountyList() {
      this.formData.county = null;
      this.loading = false;
      this.queryParams.parentId = this.formData.experimentStation;
      listStation(this.queryParams).then((response) => {
        console.log(response);
        this.countyList = response.rows;
        this.loading = false;
      });
    },

    /** 查询指定示范县和年份的记录 */
    getRecordList() {
      this.queryParams.demonstrationCountyName = this.formData.county;
      this.queryParams.year = this.formData.year;
      listRecord(this.queryParams).then((response) => {
        console.log(response);
        this.recordList = response.rows;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      // this.getList();
      if (
        this.formData.county == null ||
        this.formData.county == "" ||
        this.formData.year == null ||
        this.formData.year == ""
      ) {
        this.$message.error("请选择试验站和年份后搜索");
      } else {
        this.getRecordList();
      }
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/record/export",
        {
          ...this.queryParams,
        },
        `station_${new Date().getTime()}.xlsx`
      );
    },
  },

  watch: {
    formData: {
      handler(newval, oldval) {
        console.log(newval.experimentStation);
        if (newval.province == null || newval.province == "") {
          this.stationSelectorDisabled = true;
          this.countySelectorDisabled = true;
        } else if (newval.province != null && newval.province != "") {
          this.stationSelectorDisabled = false;
        }

        if (
          newval.experimentStation == null ||
          newval.experimentStation == ""
        ) {
          this.countySelectorDisabled = true;
        } else if (
          newval.experimentStation != null &&
          newval.experimentStation != ""
        ) {
          this.countySelectorDisabled = false;
          console.log(123);
        }
      },
      deep: true,
    },
  },
};
</script>
